package mark.tom.gear.base.utils

import android.app.ActivityManager
import android.content.Context
import android.os.Process
import java.lang.reflect.Field
import java.util.regex.Pattern

object SystemUtil {

    /**
     * Description: 获取进程名
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/1/15 1:36 下午
     */
    fun getProcessName(context: Context): String? {
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val appProcesses = activityManager.runningAppProcesses
        val myPid = Process.myPid()
        if (appProcesses == null || appProcesses.size == 0) {
            return null
        }
        for (appProcess in appProcesses) {
            if (appProcess.processName == context.packageName) {
                if (appProcess.pid == myPid) {
                    return appProcess.processName
                }
            }
        }
        return null
    }

    /**
     * Description: 判断是否运行在主线程
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/1/15 1:36 下午
     */
    fun isMainProcess(context: Context): Boolean {
        return context.packageName == getProcessName(context)
    }

    /**
     * Description: 判断VM是否支持多dex
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/1/15 1:36 下午
     */
    val isVMMultiDexCapable: Boolean
        get() = isVMMultiDexCapable(System.getProperty("java.vm.version"))

    /**
     * Description: MultiDex 拷出来的的方法，判断VM是否支持多dex
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/1/15 1:35 下午
     */
    private fun isVMMultiDexCapable(versionString: String?): Boolean {
        var isMultiDexCapable = false
        if (versionString != null) {
            val matcher = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+)?").matcher(versionString)
            if (matcher.matches()) {
                try {
                    val major = matcher.group(1)?.toInt()
                    val minor = matcher.group(2)?.toInt()
                    if (major != null && minor != null) {
                        isMultiDexCapable = major > 2 || major == 2 && minor >= 1
                    }
                } catch (var5: NumberFormatException) {
                }
            }
        }
        return isMultiDexCapable
    }

    /**
     * Description: 获取BuildConfig文件内的字段数据
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/1/15 1:49 下午
     */
    fun getBuildConfigFieldValue(packageName : String, fieldName : String) : Any? {
        var result : Any? = null
        try {
            val clz : Class<*> = Class.forName("$packageName.BuildConfig")
            val field : Field = clz.getField(fieldName)
            result = field.get(null)
        } catch (e : Exception) {
            // Ignore
        }
        return result
    }

}